import time
import pandas as pd
from DrissionPage import ChromiumPage, ChromiumOptions

def create_dp():
    co = ChromiumOptions()
    co.set_browser_path(r'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe')
    co.headless(False)
    co.set_argument('--start-maximized')
    co.set_proxy('http://127.0.0.1:7890')
    co.set_user_agent(
        user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
    )
    return ChromiumPage(co)

def main():
    df = pd.read_excel('demo.xlsx')
    if 'link' not in df.columns:
        print("未找到 link 列，请检查文件")
        return

    browser = create_dp()
    try:
        for link in df['link'].dropna():
            print(link)
            browser.get(str(link))
            # 等待页面加载完全再操作
            time.sleep(3)

            # 第一次点击
            btn1 = browser.ele('x://*[@id="bili-download-btn"]')
            if btn1 is not None:
                btn1.click()
                print("已点击下载按钮")
            else:
                print("未找到下载按钮，跳过此页。")
                continue

            time.sleep(2)

            # 第二次点击
            btn2 = browser.ele('x://*[@id="select-panel"]/div[3]/button')
            if btn2 is not None:
                btn2.click()
                print("已点击select-panel下载按钮")
            else:
                print("未找到二次按钮，跳过此页。")
                continue

            print("等待一会...")
            time.sleep(15)
    finally:
        browser.quit()

if __name__ == '__main__':
    main()